package com.pig4cloud.pig.common.swagger.config;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.lang.Dict;
import lombok.AllArgsConstructor;
import org.springframework.cloud.gateway.config.GatewayProperties;
import org.springframework.cloud.gateway.route.Route;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.toList;
import static org.springframework.cloud.gateway.support.NameUtils.GENERATED_NAME_PREFIX;

/**
 * @author Sywd
 * 聚合接口文档注册，和zuul实现类似
 */
@Primary
@Component
@AllArgsConstructor
public class SwaggerProvider implements SwaggerResourcesProvider {
    private static final String API_URI = "/v2/api-docs";
    private final RouteLocator routeLocator;
    private final FilterIgnorePropertiesConfig filterIgnorePropertiesConfig;
    private final GatewayProperties gatewayProperties;

    @Override
    public List<SwaggerResource> get() {
        List routes = Stream.of(routeLocator.getRoutes()).map(routeFlux -> routeFlux.map(i->i.getId())).collect(Collectors.toUnmodifiableList());

        List<SwaggerResource> result = gatewayProperties.getRoutes().parallelStream().filter(routeDefinition -> routes.contains(routeDefinition.getId())).collect(toList())
                .stream().flatMap(it -> it.getPredicates().stream()
                                .filter(predicateDefinition -> "Path".equalsIgnoreCase(predicateDefinition.getName()))
                                // 过滤数据
                                .filter(predicateDefinition -> !filterIgnorePropertiesConfig.getSwaggerProviders().contains(it.getId()))
                                .map(predicateDefinition -> swaggerResource(it.getId(), predicateDefinition.getArgs().get(GENERATED_NAME_PREFIX + "0").replace("/**", API_URI)))
                ).sorted(comparing(SwaggerResource::getName)).collect(toList());

        return result;
    }

    private static SwaggerResource swaggerResource(String name, String location) {
        // SwaggerResource swaggerResource = new SwaggerResource(); // BeanUtil.mapToBean(new Dict().set("swaggerVersion","2.0"),SwaggerResource.class,false)
        // swaggerResource.setName(name);
        // swaggerResource.setLocation(location);
        // swaggerResource.setSwaggerVersion("2.0");
        SwaggerResource swaggerResource = BeanUtil.mapToBean(new Dict().set("swaggerVersion","2.0").set("name",name).set("location",location),SwaggerResource.class,CopyOptions.create().setIgnoreError(false).setIgnoreCase(true));
        return swaggerResource;
    }
}
